package com.yc.fruitmarket.biz.impl;

import java.util.List;
import java.util.Map;

import com.yc.fruitmarket.biz.blogtypeBiz;
import com.yc.fruitmarket.dao.DBHelper;

public class blogtypeBizImpl implements blogtypeBiz {
	private DBHelper db = new DBHelper();

	@Override
	public List<Map<String, String>> findBlogType() {
		String sql = "select * from blogtype where status = 1";
		return db.find(sql);
	}

	@Override
	public List<Map<String, String>> getBlogTypes() {
		String sql = "select blogtype.btname, blogtype.btid,count(blog.bid) as count, blogtype.`status` from blogtype left join blog on blogtype.btid = blog.btid where blogtype.`status` = 1 GROUP BY blogtype.btname, blogtype.btid";
		return db.find(sql);
	}
	
	//添加类别
			@Override
			public int add(String name) {
				String sql = "insert into blogtype values(null, ?, 1)";
				int result = db.doUpdate(sql, name);
				return result;
			}
			
			//获得类别
			@Override
			public List<Map<String, String>> gettype() {
				String sql = "select blogtype.btid, blogtype.btname, count(blog.btid) as count , blogtype.status from blogtype LEFT JOIN blog on blogtype.btid = blog.btid GROUP BY blogtype.btname,blogtype.btid";
				List<Map<String, String>> list = db.find(sql);
				return list;
			}
			
			//修改类别
			@Override
			public int updatetype(int btid, String btname) {
				String sql = "update blogtype set btname = ? where btid = ?";
				return db.doUpdate(sql, btname, btid);
			}
			
			//查询类别
			@Override
			public List<Map<String, String>> getbytype(String btname, String status) {
				String sql = "select blogtype.btid, blogtype.btname, count(blog.btid) as count , blogtype.status from blogtype LEFT JOIN blog on blogtype.btid = blog.btid where 1 = 1";
				if(!"".equals(status) && status != null) {
					if(!"-1".equals(status)) {
						sql += " and blogtype.status = " + status;
					}
				}
				if(btname != null || btname != "") {
					sql += " and btname like '%"+btname+"%'";
				}
				sql += " GROUP BY blogtype.btname,blogtype.btid";
				List<Map<String, String>> list = db.find(sql);
				return list;
			}
		
		//上架类别
		@Override
		public int up(String btid) {
			String sql = "update blogtype set status = 1 where btid = ?";
			return db.doUpdate(sql, btid);
		}
		
		//下架类别
		@Override
		public int down(String btid) {
			String sql = "update blogtype set status = 0 where btid = ?";
			return db.doUpdate(sql, btid);
		}
	
	
}
